package net.asfun.jvalog.control;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.asfun.jvalog.service.ConfigService;
import net.asfun.jvalog.service.SiteService;
import net.asfun.jvalog.util.FileUtil;
import net.asfun.jvalog.util.RequestUtil;
import net.asfun.jvalog.util.ResponseUtil;
import net.asfun.jvalog.vo.Blog;
import net.asfun.jvalog.vo.Setting;
import net.asfun.jvalog.mvc.RequestMapping;

public class ConsoleController {


	@RequestMapping({"/index.html", "/", ""})
	public String index(HttpServletRequest req, Map<String, Object> model) {
		return "index.html";
	}
	
	@RequestMapping("/config")
	public String config(HttpServletRequest req, Map<String, Object> model) {
		model.put("configs", ConfigService.listAll());
		return "configs.html";
	}
	
	@RequestMapping("/config/update")
	public String updateConfig(HttpServletRequest req, Map<String, Object> model) {
		Long id = RequestUtil.getLongParam(req, "id");
		String value = RequestUtil.getRequiredParam(req, "value");
		ConfigService.update(id, value);
		return ResponseUtil.feedback("config.update", req, model);
	}
	
	@RequestMapping("/site")
	public String site(HttpServletRequest req, Map<String, Object> model) {
		String[] themes = FileUtil.listDir(Setting.WEB_ROOT + "/themes/");
		model.put("themes", themes);
		model.put("blog", Blog.info());
		return "site.html";
	}
	
	@RequestMapping("/site/update")
	public String updateSite(HttpServletRequest req, Map<String, Object> model) {
		Blog site = Blog.info();
		site.setTitle(RequestUtil.getRequiredParam(req, "title"));
		site.setSubtitle(RequestUtil.getRequiredParam(req, "subtitle"));
		site.setKeywords(RequestUtil.getStringParam(req, "keywords"));
		site.setDescription(RequestUtil.getRequiredParam(req, "description"));
		site.setUrl(RequestUtil.getRequiredParam(req, "url"));
		site.setTheme(RequestUtil.getRequiredParam(req, "theme"));
		site.setVersion(RequestUtil.getRequiredParam(req, "version"));

		SiteService.update(site);
		
		return ResponseUtil.feedback("blog.update", req, model);
	}
}
